/**
 * Created by admin on 2017/10/30.
 */
/**
 * Created by admin on 2017/10/26.
 */
$(document).ready(function() {
    $("form").submit(function(){
        return false;
    });
    var VAL ={
        NORMAL       : "0000000"  ,
        EVT_USER     : $("#userId").text(),
        T_FBPBISOPE	 : 'FBPBISOPE',
        T_FBPBISTOL  : 'FBPBISTOL',
        T_FDPAPLYWO  : 'FDPAPLYWO',
        T_FBPMDLDEF  : 'FBPMDLDEF',
        T_FIPINQBOX  : 'FIPINQBOX',
        T_FPPOPACKCLM : 'FPPOPACKCLM',
        DISABLED_ATTR : {
            'disabled' : true
        },
        ENABLED_ATTR : {
            'disabled' : false
        }
    };
    var domObj = {
        buttons : {
            $query_btn: $("#query_btn"),
            $save_btn : $("#save_btn"),
            $clean_btn: $("#clean_btn"),
        },
        $woTypeFk    : $("#woTypeFk"),
        $woIdFk      : $("#woIdFk"),


        $inBoxIdFk1    : $("#inBoxIdFk1"),
        $boxIdFk1      : $("#boxIdFk1"),
        $mdlIdFk1      : $("#mdlIdFk1"),
        $woIdFk1       : $("#woIdFk1"),
        $prdGrade1     : $("#prdGrade1"),
        $prdQty1       : $("#prdQty1"),

        $inBoxIdFk2    : $("#inBoxIdFk2"),
        $boxIdFk2      : $("#boxIdFk2"),
        $mdlIdFk2      : $("#mdlIdFk2"),
        $woIdFk2       : $("#woIdFk2"),
        $prdGrade2     : $("#prdGrade2"),
        $prdQty2       : $("#prdQty2"),

        $outBoxIdFk     : $("#outBoxIdFk"),


        grids : {
            $boxListDiv    : $("#boxListDiv"),
            $boxListGrd    : $("#boxListGrd"),
            $boxListPg     : $("#boxListPg"),

            $inBoxListDiv     : $("#inBoxListDiv"),
            $inBoxListGrd     : $("#inBoxListGrd"),
            $inBoxListPg  :  $("#inBoxListPg"),
        },
        dialogs : {
            $WeightDialog      : $("#WeightDialog"),
            $nWeight           : $("#nWeight"),
            $gWeight           : $("#gWeight"),
            $weightSureBtn     : $("#weightSureBtn"),
            $weightCancelBtn   : $("#weightCancelBtn"),
        },
    };
    var print;//打印全局变量；
    function initInner() {
        var boxListCM = [
            {name: 'box_id_fk',   index: 'box_id_fk',   	label: W_BOX_ID_TAG ,    width: 180},
            {name: 'mdl_id_fk',   index: 'mdl_id_fk',   	label: PRODUCT_ID_TAG,    width: 180},
            {name: 'wo_id_fk',    index: 'wo_id_fk',   	    label: WO_ID_TAG,      width: 180},
            {name: 'prd_grade',   index: 'prd_grade',     	label: PRD_GRADE_TAG ,    width: 180},
            {name: 'prd_qty',     index: 'prd_qty',   	    label: PRD_QTY_TAG,    width: 180},
            {name: 'box_cnt',     index: 'box_cnt',   	    label: PACK_QTY_TAG,    width: 180 , hidden:true},
            /*{name: 'box_id_fk1',  index: 'bo_id_fk1',   	label: '内箱箱号1' ,   width: 180},
            {name: 'box_id_fk2',  index: 'bo_id_fk2',   	label: '内箱箱号2' ,   width: 180},*/
        ];
        domObj.grids.$boxListGrd.jqGrid({
            url:"",
            datatype:"local",
            mtype:"POST",
            height:300,
            width:domObj.grids.$boxListDiv.width(),
            autowidth : false,
            shrinkToFit : false,
            resizable : true,
            loadonce : true,
            viewrecords : true,
            rownumbers : true,
            rowNum : 20,
            emptyrecords : true,
           // multiselect : true,
            pager : domObj.$boxListPg,
            colModel: boxListCM,
            onSelectRow : function(rowId) {
                var box_id_fk,wo_id_fk,iary,inObj,outObj;
                var  rowData = domObj.grids.$boxListGrd.jqGrid("getRowData",rowId);
                box_id_fk = rowData.box_id_fk;
                iary = {
                    wbox_id : box_id_fk,
                    //wo_id_fk  : wo_id_fk
                }
                inObj = {
                    trx_id : VAL.T_FPPOPACKCLM,
                    action_flg   : 'F',
                    iary : [iary]
                };
                var outObj = comTrxSubSendPostJson(inObj);
                if (outObj.rtn_code == VAL.NORMAL) {
                    setGridInfo(outObj.oary, "#inBoxListGrd");
                }
            }
        });
    };

    function initInBox(){
        var boxListCM = [
            {name: 'box_id',   index: 'box_id',   	        label: N_BOX_ID_TAG,    width: 150},
            {name: 'mdl_id_fk',   index: 'mdl_id_fk',   	label: PRODUCT_ID_TAG,    width: 150},
            {name: 'wo_id_fk',    index: 'wo_id_fk',   	    label: WO_ID_TAG,      width: 150},
            {name: 'prd_grade',   index: 'prd_grade',     	label: PRD_GRADE_TAG ,    width: 150},
            {name: 'prd_qty',     index: 'prd_qty',   	    label: PRD_QTY_TAG,    width: 150},
        ];
        domObj.grids.$inBoxListGrd.jqGrid({
            url:"",
            datatype:"local",
            mtype:"POST",
            height:200,
            width:domObj.grids.$inBoxListDiv.width(),
            autowidth : false,
            shrinkToFit : false,
            resizable : true,
            loadonce : true,
            viewrecords : true,
            rownumbers : true,
            rowNum : 20,
            emptyrecords : true,
            // multiselect : true,
            pager : domObj.grids.$inBoxListPg,
            colModel: boxListCM,
            onSelectRow: function(id){
                var selRowId = domObj.grids.$inBoxListGrd.jqGrid("getGridParam","selrow");
                var rowData = domObj.grids.$inBoxListGrd.jqGrid("getRowData",selRowId);
                domObj.$boxIdFk1.html(rowData.box_id);
                domObj.$mdlIdFk1.html(rowData.mdl_id_fk);
                domObj.$woIdFk1.html(rowData.wo_id_fk);
                domObj.$prdGrade1.html(rowData.prd_grade);
                domObj.$prdQty1.html(rowData.prd_qty);
            }
        });
    };

    var initSelectFnc = {
        initWoType: function () {
            comAddValueByDataCateFnc("#woTypeFk", "WOCT", "data_ext", "data_desc", true);
        },
        iniWoIdSel :function(){
            var wo_cate = domObj.$woTypeFk.find("option:selected").val();
            var inObj = {
                trx_id : VAL.T_FDPAPLYWO,
                action_flg : "Q",
                iary : [{
                    wo_cate  : wo_cate
                }]
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                SelectDom.addSelectArr(domObj.$woIdFk, outObj.oary, "wo_id","wo_id","",true);
            }
        },
    }
    var toolFunc = {
        query_func : function() {
            var box_id_fk,wo_id_fk,iary,inObj,outObj;
                box_id_fk = domObj.$outBoxIdFk.val();
                //wo_id_fk  = domObj.$woIdFk.val();
            iary = {
                wbox_id : box_id_fk,
                //wo_id_fk  : wo_id_fk
            }
            inObj = {
                trx_id : VAL.T_FPPOPACKCLM,
                action_flg   : 'Q',
                iary : [iary]
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                setGridInfo(outObj.oary, "#boxListGrd");
            }
        },
        save_func : function() {
            var iary,inObj,outObj;
            var wboxId = domObj.$outBoxIdFk.val();
            var rowIds = domObj.grids.$inBoxListGrd.jqGrid("getDataIDs");
            if(rowIds.length == 0){
                showErrorDialog("","内部箱号列表为空，请先添加内部箱号！");
                return false;
            }
            var inBoxIds = new Array();
            for(var i=0 ; i < rowIds.length; i++){
               var rowData =  domObj.grids.$inBoxListGrd.jqGrid("getRowData",rowIds[i]);
                inBoxIds.push(rowData.box_id);
            }
            var NWeight,GWeight;
            NWeight = domObj.dialogs.$nWeight.val();
            GWeight = domObj.dialogs.$gWeight.val();
            iary = {
                box_ids : inBoxIds,
                wbox_id : wboxId
            }
            inObj = {
                trx_id : VAL.T_FPPOPACKCLM,
                action_flg   : 'A',
                evt_usr      : VAL.EVT_USER,
                iary : [iary]
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                print = outObj.oary[0];
                setGridInfo(outObj.oary, "#boxListGrd");
                var length = rowIds.length;
                for(var i=0;i<length;i++){
                    domObj.grids.$inBoxListGrd.jqGrid("delRowData",rowIds[0]);
                }
                if(print.wo_id_fk.length==8){
                    var woIdFk = print.wo_id_fk;
                    print.wo_id_fk = woIdFk.slice(0,1)+ woIdFk.slice(2,8)
                }
                printBtnfunc(print.mdl_id_fk,print.mdl_desc,print.wo_id_fk,
                    print.prd_qty,NWeight,GWeight,print.box_id_fk);
                showSuccessDialog("合并成功");
            }
        },
        cancel_func : function() {
            var boxIdFk,selRowId,iary,inObj,outObj;
            selRowId=$('#boxListGrd').jqGrid('getGridParam','selrow');
            rowData = domObj.grids.$boxListGrd.jqGrid("getRowData", selRowId);
            boxIdFk = rowData.box_id_fk;
            if(rowData.box_id_fk==null){
                showErrorDialog("", "请选择一条记录");
                return false;
            }
            iary = {
                wbox_id:rowData.box_id_fk,
            }
            inObj = {
                trx_id : VAL.T_FPPOPACKCLM,
                action_flg   : 'D',
                evt_usr      : VAL.EVT_USER,
                iary : [iary]
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                var ids = domObj.grids.$boxListGrd.jqGrid('getDataIDs');
                for(var i=0;  i<ids.length;i++) {
                    var rowData = domObj.grids.$boxListGrd.jqGrid("getRowData", ids[i]);
                    if(rowData.box_id_fk == boxIdFk ){
                        domObj.grids.$boxListGrd.jqGrid("delRowData",ids[i]);
                    }
                }
                showSuccessDialog("取消成功");
            }
        }
    }
    domObj.buttons.$query_btn.click(toolFunc.query_func);
    domObj.buttons.$save_btn.click(toolFunc.save_func);
    domObj.buttons.$clean_btn.click(toolFunc.cancel_func);
    var initializationFunc = function() {
        initInner();
        initInBox();
    }

    domObj.$inBoxIdFk1.keydown(function(event){
        if(event.keyCode==13) {
            var  box_id_fk = domObj.$inBoxIdFk1.val();
            if(!box_id_fk){
                showErrorDialog("", "内箱箱号不能为空");
                return false;
            }
            //判断右边列表有没有记录
           var rowDatas =  $("#inBoxListGrd").getRowData();
            for(var i = 0 ; i< rowDatas.length ;i++){
                if(rowDatas[i].box_id === box_id_fk){
                    showErrorDialog("","内箱列表已存在该箱号[" + box_id_fk+"],请换箱号!");
                    return false;
                }
            }
            var first_wo_id ;
            if(rowDatas.length >0 ){
                first_wo_id = rowDatas[0].wo_id_fk;
            }

            var iary,inObj,outObj;
            iary = {
            }
            inObj = {
                trx_id : VAL.T_FIPINQBOX,
                action_flg   : 'M',
                box_id : box_id_fk,
                iary : [iary]
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                if(outObj.box_cate=='W'){
                    showErrorDialog("","该箱子类型为外箱!");
                    return false;
                }
                domObj.$boxIdFk1.text(outObj.box_id);
                domObj.$mdlIdFk1.text(outObj.mdl_id_fk);
                domObj.$woIdFk1.text(outObj.wo_id_fk);
                domObj.$prdGrade1.text(outObj.prd_grade_fk);
                domObj.$prdQty1.text(outObj.prd_qty);
                var rowdata = {};
                rowdata.box_id = outObj.box_id;
                rowdata.mdl_id_fk = outObj.mdl_id_fk;
                rowdata.wo_id_fk = outObj.wo_id_fk;
                if(first_wo_id && outObj.wo_id_fk !== first_wo_id){
                    showErrorDialog("","工单必须一致！");
                    return;
                }
                rowdata.prd_grade = outObj.prd_grade_fk;
                rowdata.prd_qty = outObj.prd_qty;

                var newRowId = getGridNewRowID("#inBoxListGrd");
                domObj.grids.$inBoxListGrd.jqGrid("addRowData", newRowId,
                        rowdata);
                domObj.$inBoxIdFk1.val("");//清空输入框
            }
        }
    }),
        domObj.$outBoxIdFk.keydown(function(event){
            if(event.keyCode==13) {
                toolFunc.query_func();
            }
        }),
    initializationFunc();
    //表格自适应
    function resizeFnc(){
        var offsetBottom, grdDivWidth;
        grdDivWidth = $("#topDiv").width();
        offsetBottom = $(window).height() - $("#boxListDiv").offset().top;

        $("#boxListDiv").width(grdDivWidth*0.98);
        $("#boxListDiv").height(offsetBottom*0.99);
        $("#boxListDiv").setGridWidth(grdDivWidth*0.97);
        $("#boxListDiv").setGridHeight(offsetBottom*0.95-51);
    };

    resizeFnc();
    $(window).resize(function() {
        resizeFnc();
    });
});
